package net.mingsoft.base.action;

import org.slf4j.*;
import net.mingsoft.base.util.*;
import javax.servlet.http.*;
import java.net.*;
import org.apache.commons.lang3.*;
import java.util.*;

public abstract class BaseAction
{
    protected final Logger LOG;
    private static String[] mobileGateWayHeaders;
    private static String[] pcHeaders;
    private static String[] mobileUserAgents;
    
    public BaseAction() {
        this.LOG = LoggerFactory.getLogger((Class)this.getClass());
    }
    
    @Deprecated
    protected String getLocaleString(final String key, final String resources) {
        return BundleUtil.getLocaleString(key, resources);
    }
    
    @Deprecated
    protected String getUrl(final HttpServletRequest request) {
        final String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName();
        if (request.getServerPort() == 80) {
            basePath += path;
        }
        else {
            basePath = basePath + ":" + request.getServerPort() + path;
        }
        return basePath;
    }
    
    @Deprecated
    protected String getDomain(final HttpServletRequest request) {
        final String path = request.getContextPath();
        String domain = request.getServerName();
        if (request.getServerPort() == 80) {
            domain += path;
        }
        else {
            domain = domain + ":" + request.getServerPort() + path;
        }
        return domain;
    }
    
    protected String getHost(final HttpServletRequest request) {
        String basePath = request.getServerName();
        if (request.getServerPort() != 80) {
            basePath = basePath + ":" + request.getServerPort();
        }
        return basePath;
    }
    
    protected String getHostIp() {
        try {
            final InetAddress addr = InetAddress.getLocalHost();
            return addr.getHostAddress();
        }
        catch (UnknownHostException e) {
            e.printStackTrace();
            return "";
        }
    }
    
    @Deprecated
    protected String getResString(final String key) {
        return BundleUtil.getResString(key);
    }
    
    @Deprecated
    protected String getResString(final String key, final ResourceBundle rb) {
        return BundleUtil.getResString(key, rb);
    }
    
    protected String getResString(final String key, final String... fullStrs) {
        return BundleUtil.getResString(key, fullStrs);
    }
    
    @Deprecated
    protected String getResString(final String key, final ResourceBundle rb, final String... fullStrs) {
        return BundleUtil.getResString(key, rb, fullStrs);
    }
    
    public boolean isMobileDevice(final HttpServletRequest request) {
        boolean b = false;
        boolean pcFlag = false;
        boolean mobileFlag = false;
        final String via = request.getHeader("Via");
        final String userAgent = request.getHeader("user-agent");
        for (int i = 0; via != null && !via.trim().equals("") && i < BaseAction.mobileGateWayHeaders.length; ++i) {
            if (via.contains(BaseAction.mobileGateWayHeaders[i])) {
                mobileFlag = true;
                break;
            }
        }
        for (int i = 0; !mobileFlag && userAgent != null && !userAgent.trim().equals("") && i < BaseAction.mobileUserAgents.length; ++i) {
            if (userAgent.contains(BaseAction.mobileUserAgents[i])) {
                mobileFlag = true;
                break;
            }
        }
        for (int i = 0; userAgent != null && !userAgent.trim().equals("") && i < BaseAction.pcHeaders.length; ++i) {
            if (userAgent.contains(BaseAction.pcHeaders[i])) {
                pcFlag = true;
                break;
            }
        }
        if (mobileFlag && !pcFlag) {
            b = true;
        }
        return b;
    }
    
    protected Map<String, String> getMapByProperties(final String filePath) {
        if (StringUtils.isBlank((CharSequence)filePath)) {
            return null;
        }
        final ResourceBundle rb = ResourceBundle.getBundle(filePath);
        return this.getMapByProperties(rb);
    }
    
    protected Map<String, String> getMapByProperties(final ResourceBundle rb) {
        final Map<String, String> map = new HashMap<String, String>();
        final Enumeration<String> en = rb.getKeys();
        while (en.hasMoreElements()) {
            final String key = en.nextElement();
            map.put(key, rb.getString(key));
        }
        return map;
    }
    
    static {
        BaseAction.mobileGateWayHeaders = new String[] { "ZXWAP", "chinamobile.com", "monternet.com", "infoX", "XMS 724Solutions HTG", "wap.lizongbo.com", "Bytemobile" };
        BaseAction.pcHeaders = new String[] { "Windows 98", "Windows ME", "Windows 2000", "Windows XP", "Windows NT", "Ubuntu" };
        BaseAction.mobileUserAgents = new String[] { "Nokia", "SAMSUNG", "MIDP-2", "CLDC1.1", "SymbianOS", "MAUI", "UNTRUSTED/1.0", "Windows CE", "iPhone", "iPad", "Android", "BlackBerry", "UCWEB", "ucweb", "BREW", "J2ME", "YULONG", "YuLong", "COOLPAD", "TIANYU", "TY-", "K-Touch", "Haier", "DOPOD", "Lenovo", "LENOVO", "HUAQIN", "AIGO-", "CTC/1.0", "CTC/2.0", "CMCC", "DAXIAN", "MOT-", "SonyEricsson", "GIONEE", "HTC", "ZTE", "HUAWEI", "webOS", "GoBrowser", "IEMobile", "WAP2.0" };
    }
}
